﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using WebAppPlanProduccion.Code.Security;
using Web.Framework;
using PlanProd.Contracts.Misc;
using PlanProd.Contracts.Search;
using PlanProd.Contracts;

namespace WebAppPlanProduccion.App
{
    public partial class ProveedorList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.PROVEEDOR_VER);           
        }

        protected override void ApplySecurity()
        {
            this.lnkSincronizar.Visible = HasPermissionFor(PlanProdSecurity.PROVEEDOR_SINCRONIZAR);
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkSincronizar_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    Buscar();
            //}
            //catch (Exception ex)
            //{
            //    ExceptionManager.Instance.HandleException(ex);
            //}
        }

        protected void GridProveedores_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridProveedores.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridProveedores_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void GridProveedores_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        private void Buscar()
        {
            ServiceOptions searchOptions = new ServiceOptions();
            searchOptions.PageNumber = GridProveedores.PageIndex;
            searchOptions.PageSize = GridProveedores.PageSize;

            SearchProveedorCriteria productSearchCriteria = new SearchProveedorCriteria();
            productSearchCriteria.EmpresaId = BaseMasterPage.CompanyId;
            productSearchCriteria.RazonSocial = this.txtRazonSocial.Text;

            List<Proveedor> proveedores = WebServiceHelper.GetProveedores(searchOptions, productSearchCriteria);

            this.GridProveedores.DataSource = proveedores;
            this.GridProveedores.DataBind();

            pnlResults.Visible = true;
        }
    }
}